﻿using System;
using System.Text;

namespace AlgorytmyGenetyczne
{
    public class Genotyp : IEquatable<Genotyp>
    {
        public bool[] Geny { get; set; }

        public bool this[int i]
        {
            get
            {
                return Geny[i];
            }
            set
            {
                Geny[i] = value;
            }
        }

        public Genotyp(){}

        public Genotyp(Genotyp genotyp)
        {
            Geny = new bool[genotyp.Length];
            for(int i = 0; i<Geny.Length; i++)
            {
                Geny[i] = genotyp[i];
            }
        }

        public int Length { get { return Geny.Length; } }

        public bool Equals(Genotyp other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Geny, Geny);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(Genotyp)) return false;
            return Equals((Genotyp)obj);
        }

        public override int GetHashCode()
        {
            return (Geny != null ? Geny.GetHashCode() : 0);
        }

        public static bool operator ==(Genotyp left, Genotyp right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Genotyp left, Genotyp right)
        {
            return !Equals(left, right);
        }

        public static Genotyp operator +(Genotyp left, Genotyp right)
        {
            int leftLength = left.Geny.Length;
            int rightLength = right.Geny.Length;
            bool[] polaczoneGeny = new bool[leftLength + rightLength];

            for (int i = 0; i < polaczoneGeny.Length; i++)
            {
                if (i < leftLength)
                {
                    polaczoneGeny[i] = left[i];
                }
                else
                {
                    polaczoneGeny[i] = right[i - leftLength];
                }
            }
            return new Genotyp { Geny = polaczoneGeny };
        }

        public Genotyp fragmentGenotypu(int startIndex, int endIndex)
        {
            bool[] fragment = new bool[(endIndex - startIndex) + 1];
            int indeksFragmentu = 0;
            for (int i = startIndex; i <= endIndex; i++)
            {
                fragment[indeksFragmentu] = Geny[i];
                indeksFragmentu++;
            }
            return new Genotyp { Geny = fragment };
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder("[");
            for (int i = Geny.Length - 1; i > -1; i--)
            {
                stringBuilder.Append(Geny[i] ? "1" : "0");
            }
            stringBuilder.Append("]");
            return stringBuilder.ToString();
        }

        public string wyswietlNieodwrocony()
        {
            StringBuilder stringBuilder = new StringBuilder("[");
            for (int i = 0; i < Geny.Length; i++)
            {
                stringBuilder.Append(Geny[i] ? "1" : "0");
            }
            stringBuilder.Append("]");
            return stringBuilder.ToString();
        }
    }
}
